package com.hzhc.controller.baseinfo;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.CarInfo;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.CarInfoService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.CarInfoVo;
import com.hzhc.vo.CntrInfoVo;
import com.hzhc.vo.CompanyInfoVo;
import com.hzhc.vo.LockVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 车辆信息 前端控制器
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/car_info")
public class CarInfoController extends BaseController {
    private  final CarInfoService carInfoService;


    /**
     * 车牌格式
     * @param request
     *
     * @return
     */
    @RequestMapping("/getCarNo")
    public ResultUtil<T> getOrganzationTree(HttpServletRequest request, @RequestBody CarInfo carInfo){
        return carInfoService.selectCarInNo(carInfo);
    }

    //车辆黑名单校验
    @RequestMapping("/checkIsBlack")
    public ResultUtil<T> checkIsBlack(@RequestBody CarInfo carInfo){
        return carInfoService.checkIsBlack(carInfo);
    }

    /**
     * 车辆信息列表
     * @return
     */
    @RequestMapping("/getCarInfoList")
    public ResultUtil<List<CarInfo>> getCarInfoList(@RequestBody CarInfoVo vo){
        Page<CarInfo> page =new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<CarInfo> list =carInfoService.getList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 车辆信息新增
     */
    @RequestMapping("/saveCarInfo")
    @SysTemLog(operModul = "基础资料-车辆添加", operType = "添加", operDesc = "车辆添加功能", modular = "PC")
    public ResultUtil<T> saveCarInfo(HttpServletRequest httpServletRequest,@RequestBody CarInfo carInfo) {
        UserDto user= TokenUtil.getUser(httpServletRequest);
        carInfo.setUserId(user.getId());
        return carInfoService.addRet(carInfo);
        }
    /**
     * 车辆信息编辑
     */
    @RequestMapping("/editCarInfo")
    @SysTemLog(operModul = "基础资料-车辆编辑", operType = "编辑", operDesc = "车辆编辑功能", modular = "PC")
    public ResultUtil<T> editCarInfo(@RequestBody CarInfo carInfo){
        return carInfoService.updateRet(carInfo);
    }
    /**
     * 车辆信息删除
     */
    @RequestMapping("/delCarInfo/{id}")
    @SysTemLog(operModul = "基础资料-车辆删除", operType = "删除", operDesc = "车辆删除功能", modular = "PC")
    public ResultUtil<T>  delCarInfo(@PathVariable("id") Long id) {
        return carInfoService.delCarInfo(new CarInfo().setId(id));
    }
    /**
     * 查询车队信息列表
     */
    @RequestMapping("/getCarTeamPage")
    public ResultUtil<List<CompanyInfoVo>> getCarTeamPage(@RequestBody CompanyInfoVo vo) {
        Page<CompanyInfoVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<CompanyInfoVo> list = carInfoService.getCarTeamPage(page, vo);
        return ResultUtil.build(list,page.getTotal(), page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }
    /**
     * 车辆导入
     * 保存上传的车辆
     */
    @PostMapping("/importCarList")
    public ResultUtil<T> importCarList(HttpServletRequest request, @RequestBody CarInfoVo vo) {
        return carInfoService.importCarList(request, vo.getCarList());
    }
    /**
     * 车辆清单导出模板下载
     */
    private final static String EXPORT_EXCEL_PATH_CMS = "static/download/车辆导入.xlsx";

    @GetMapping("/exportCarFile")
    public void exportCarFile(HttpServletRequest request, HttpServletResponse response) {
        carInfoService.exportCarFile(request, response, EXPORT_EXCEL_PATH_CMS);
    }

    /**
     *车辆导入
     */
    @RequestMapping("/carUpload")
    public ResultUtil<List<CarInfoVo>> carUpload(MultipartFile file, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        return carInfoService.CarUpload(user,file);
    }
}

